#pragma once

// 基于MySQL的Model模块

#include <iostream>
#include <string>
#include <unordered_map>
#include <assert.h>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include "../comm/log.hpp"
#include "../comm/util.hpp"
#include "include/mysql.h"

namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        std::string number; // 题目编号
        std::string title;  // 题目标题
        std::string level;  // 题目等级
        std::string desc;   // 题目描述
        std::string header; // 题目预设代码
        std::string tail;   // 测试用例
        int cpu_limit;      // 时间限制
        int mem_limit;      // 空间限制
    };

    const std::string oj_questions = "oj_questions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "Sunnygu@050618";
    const std::string db = "oj";
    const int port = 3306;

    class Model
    {
    private:
        MYSQL *my = nullptr;

    public:
        Model() 
        {
            assert(ConnentMysql());
        }

        bool ConnentMysql()
        {
            // 创建mysql句柄
            my = mysql_init(nullptr);

            // 连接数据库，判断是否连接成功
            if (nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << " model模块: 连接数据库失败!" << std::endl;
                return false;
            }
            mysql_set_character_set(my, "utf8"); //设置连接的编码格式
            LOG(INFO) << " model模块: 连接数据库成功" << std::endl;
            return true;
        }

        // 访问数据库
        bool QueryMysql(const std::string &sql, std::vector<Question> *out)
        {
            // 执行sql语句，判断是否查询成功
            if(0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARNING) << " model模块: 查询失败" << std::endl;
                return false;
            }

            // 获取结果
            MYSQL_RES *res = mysql_store_result(my);
            int rows = mysql_num_rows(res);
            int cols = mysql_num_fields(res);

            Question q;
            for(int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                q.number = row[0];
                q.title = row[1];
                q.level = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = atoi(row[6]);
                q.mem_limit = atoi(row[7]);
                out->push_back(q);
            }

            // 释放内存
            free(res);
            return true;
        }

        // 获取所有题目信息
        bool GetAllQuestions(std::vector<Question> *out)
        {
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMysql(sql, out);
        }

        // 获取某个题目信息
        bool GetOneQuestion(const std::string &number, Question *q)
        {
            bool res = false;
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where number=";
            sql += number;
            std::vector<Question> result;
            if (QueryMysql(sql, &result))
            {
                if (result.size() == 1)
                {
                    *q = result[0];
                    res = true;
                }
            }
            return res;
        }

        ~Model() 
        {
            // 关闭mysql连接
            mysql_close(my);
        }
    };
}